package com.sttk.secreconeclick.bookkeep.utils;

import android.util.Log;

import com.blankj.utilcode.util.TimeUtils;
import com.sttk.secreconeclick.bookkeep.bean.BillBean;
import com.sttk.secreconeclick.bookkeep.bean.BillListBean;
import com.sttk.secreconeclick.bookkeep.bean.YearlyStatisticsBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBillReader {
    public static List<BillListBean> getSortDate(List<BillBean> listData, String sTime, int cycle) {
        List<BillListBean> listDay = new ArrayList<>();
        SimpleDateFormat format = null;
        if (cycle == 0) {
            format = new SimpleDateFormat("yyyy年M月 MM月dd日，EEE");//统一日期格式
        } else if (cycle == 1) {
            format = new SimpleDateFormat("yyyy年 MM月dd日，EEE");//统一日期格式
        } else if (cycle == 2) {
            format = new SimpleDateFormat("yyyy年 MM月dd日，EEE");//统一日期格式
        }
        //使用 Map 来按照日期将 Event 分别存储到对应日期的 List 中
        Map<String, List<BillBean>> eventMap = new HashMap<>();
        for (BillBean bean : listData) {
            String date = TimeUtils.millis2String(bean.getDate(), format);
            if (!eventMap.containsKey(date)) {
                eventMap.put(date, new ArrayList<>());
            }
            eventMap.get(date).add(bean);
        }
        //打印每个日期对应的 Event 列表
        for (Map.Entry<String, List<BillBean>> entry : eventMap.entrySet()) {
            String[] s1 = entry.getKey().split(" ");
            double bPut = 0.00;
            double bOut = 0.00;
            for (BillBean b : entry.getValue()) {
                double f1 = b.getMoney();
                if (b.isIncome()) {
                    bPut += f1;
                } else {
                    bOut += f1;
                }
            }
            if (cycle == 2) {
                listDay.add(new BillListBean(TimeUtils.string2Millis(entry.getKey(), format), bOut, bPut, entry.getValue()));//添加到总的集合
            } else {
                if (sTime.equals(s1[0])) {
                    listDay.add(new BillListBean(TimeUtils.string2Millis(entry.getKey(), format), bOut, bPut, entry.getValue()));//添加到总的集合
                }
            }
        }
        //降序排序
        Collections.sort(listDay, new Comparator<BillListBean>() {
            @Override
            public int compare(BillListBean e1, BillListBean e2) {
                return Long.compare(e2.getDate(), e1.getDate());
            }
        });
        return listDay;
    }

    /**
     * 获取指定年份的账单统计，每月数据包含月份、收入、支出、结余
     *
     * @param listData 账单数据列表
     * @return 年度统计列表，按月份升序排列
     */
    public static List<YearlyStatisticsBean> getYearlyStatistics(List<BillBean> listData, int year) {
        List<YearlyStatisticsBean> yearlyStats = new ArrayList<>();

        // 创建每月的统计对象，初始值都为0
        for (int month = 1; month <= 12; month++) {
            yearlyStats.add(new YearlyStatisticsBean(month, 0.0, 0.0));
        }

        // 遍历账单数据，按月份统计收入和支出
        for (BillBean bean : listData) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(bean.getDate());

            int billYear = calendar.get(Calendar.YEAR);
            int billMonth = calendar.get(Calendar.MONTH) + 1; // Calendar.MONTH 从0开始

            // 只统计指定年份的数据
            if (billYear == year) {
                YearlyStatisticsBean monthStats = yearlyStats.get(billMonth - 1);

                if (bean.isIncome()) {
                    // 收入
                    monthStats.setIncome(monthStats.getIncome() + bean.getMoney());
                } else {
                    // 支出
                    monthStats.setExpenditure(monthStats.getExpenditure() + bean.getMoney());
                }
            }
        }

        return yearlyStats;
    }
}
